import {nanoid} from "nanoid";
import axios from "axios";

const personOptions = {
    namespaced: true,
    actions:{},
    mutations:{
        ADD_PERSON(state,value){
            if (!value){
                alert('请输入姓名')
                return
            }
            state.personList.unshift(value);
        },
        addWang(state,value){
            console.log(value);
            if (!value || !(value.startsWith("王"))){
                alert('请输入姓王的人名')
                return
            }
            state.personList.unshift({id: nanoid(4), name: value});
        },
        addNameFromServer(state,value){
            console.log('==> addNameFromServer');
            axios.get('http://localhost:8080/atguigu/request/person/getPersonName')
                            .then(
                                response=>{
                                    console.log(response.data.data);
                                    state.personList.unshift({id: nanoid(4), name: response.data.data});
                                },
                                err=>{
                                    console.log(err)
                                }
                            )
        }
    },
    state:{
        personList: [
            {id:'001', age: 18, name:'张三'}
        ]
    },
    getters:{

    }
}

export default personOptions;
